package com.animal.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.animal.bean.ManagerBean;
import com.animal.dao.ManagerManage;

@WebServlet(name="ChangePasswordServlet",urlPatterns={"/ChangePasswordServlet"})
public class ChangePasswordServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public ChangePasswordServlet() {
		super();
	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();		
		
		//获取LoginCheck.jsp页面中的input
		String ID=request.getParameter("ID");

		ManagerBean manager = ManagerManage.selectByID(ID);

		String oldpassword=request.getParameter("OldPassword");
		String newpassword=request.getParameter("NewPassword");
		String checkpassword=request.getParameter("CheckPassword");
		
		//判断修改条件
		int error=ManagerManage.updatepassword(ID,oldpassword,newpassword,checkpassword);		
		if(error==0){
			out.print("<script>alert('密码修改成功!');</script>");
			out.print("<script>window.location.href='PaperChangePassword.jsp';</script>");
		}	
		else if(error==1){
			out.print("<script>alert('新密码与旧密码相同!');</script>");
			out.print("<script>window.location.href='PaperChangePassword.jsp';</script>");
		}
		else if(error==2){
			out.print("<script>alert('密码修改失败!');</script>");
			out.print("<script>window.location.href='PaperChangePassword.jsp';</script>");
		}else if(error==3){
			out.print("<script>alert('两次输入的密码不一致!');</script>");
			out.print("<script>window.location.href='PaperChangePassword.jsp';</script>");
		}
		else if(error==4){
			out.print("<script>alert('旧密码不正确!');</script>");
			out.print("<script>window.location.href='PaperChangePassword.jsp';</script>");
		}
		
        out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}